﻿using DataAccess;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class CartsBusiness
    {

        public DataTable GetData()
        {
            DataTable dt = null;
            string query = "select * from carts";
            dt = DA.GetDataTable(query, CommandType.Text, null, null);
            return dt;
        }

        public DataTable SearchData(CartsEntity ce)
        {
            DataTable dt = null;
            string query = "select * from carts where username like @username or productname like @productname";
            string[] paras = { "@username", "@productname" };
            object[] values = { "%" + ce.Username + "%", "%" + ce.Productname + "%" };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public DataTable GetAllData(CartsEntity ce)
        {
            DataTable dt = null;
            string query = "select * from carts where username = @username";
            string[] paras = { "@username" };
            object[] values = { ce.Username };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public DataTable SearchAllData(CartsEntity ce)
        {
            DataTable dt = null;
            string query = "select * from carts where username = @username and productname like @productname";
            string[] paras = { "@username", "@productname" };
            object[] values = { ce.Username, "%" + ce.Productname + "%" };
            dt = DA.GetDataTable(query, CommandType.Text, paras, values);
            return dt;
        }

        public int AddCart(CartsEntity ce)
        {
            int res = 0;
            string query = "insert into carts values (@username,@productname,@quantity,@price)";
            string[] paras = { "@username", "@productname", "@quantity", "@price" };
            object[] values = { ce.Username, ce.Productname, ce.Quantity, ce.Price };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }

        public int DeleteCart(string sql)
        {
            int res = 0;
            string query = "delete from carts where 1>1 " + sql;
            res = DA.ExcuteSql(query, CommandType.Text, null, null);
            return res;
        }

        public int DeleteCartById(CartsEntity ce)
        {
            int res = 0;
            string query = "delete from carts where cartid = @cartid";
            string[] paras = { "@cartid" };
            object[] values = { ce.Cartid };
            res = DA.ExcuteSql(query, CommandType.Text, paras, values);
            return res;
        }
    }
}
